home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7143 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.voicenet.com!news
  2. From: kobak@voicenet.com (Peter Kobak)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Hints on program optimization needed
  5. Date: 21 Feb 1996 23:52:38 GMT
  6. Organization: Voicenet - Internet Access - (215)674-9290
  7. Message-ID: <4ggb86$65m@news.voicenet.com>
  8. NNTP-Posting-Host: ivyland357.voicenet.com
  9. X-Newsreader: NeoLogic News for OS/2 [version: 4.2]
  10.  
  11. In message <3126ec6c.6806559@nntp.ix.netcom.com> - BHaddock@ix.netcom.com 
  12. (Brian F. Haddock) writes:
  13. :>
  14. :>I'm interested in any hints/tips you could provide on program
  15. :>optimization.  Nothing real fancy, just simple little 'tricks' that
  16. :>enhance program performance.
  17. :>
  18. :>For instance, what's more efficient - a if/then/else construct or a
  19. :>select/case?  How about virtual functions? You get the picture.
  20.  
  21. At the risk of sounding like a wise guy, program optimization is best 
  22. accomplished through the use of appropriate algorithms, a smart compiler, and 
  23. then examination of the slowest parts of the code. You will (usually) gain 
  24. almost nothing by universally applying "rules" about language facilities. 
  25.  
  26. Code it in the most straightforward, natural way possible. Then, find the 
  27. slow parts. Change the algorithm so you get better performance at the risk 
  28. of worse space and complexity - by far the biggest gains to be had in most 
  29. non-trivial programs are algorithm changes. Only at the very end of the 
  30. process, when you're sure you've squeezed everything out of your code 
  31. structure and only a few dozen lines take the bulk of your time, should you 
  32. consider choices like if() versus switch().
  33.  
  34. Since I don't want to seem like a total jerk, to answer your 2 specific 
  35. examples: 
  36.  
  37. if() versus switch() - depends on implementation. Except for special cases in 
  38. some compilers, there's very little difference, and it can't be generalized.
  39.  
  40. virtual functions - one additional level of indirection; that is a couple of 
  41. machine ops adding up to 5-20 clocks (< 1 micro second).
  42.  
  43. ================
  44. Peter Kobak
  45. kobak@voicenet.com
  46.  
  47.  
  48.